package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Order;
import cn.wolfcode.trip.base.service.IOrderService;
import cn.wolfcode.trip.base.util.JasonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单资源控制器
 *
 * @author Lyu
 */
@RestController
@RequestMapping("/orders")
public class OrderController {
    private final IOrderService orderService;

    @Autowired
    public OrderController(IOrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 立即兑换功能, 商品id, 商品所需积分, 订单状态
     * 资源:
     * 动作: POST
     * 参数: userId: user.id, goodsId: goods.id, totalIntegral: goods.integral, state: 1
     */
    @PostMapping
    public Object exchange(Long userId, Long goodsId, Integer totalIntegral, Integer state) {
        JasonResult result = new JasonResult();
        try {
            orderService.saveOrUpdate(userId, goodsId, totalIntegral, state);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 查询我的购物车
     */
    @GetMapping("/{userId}")
    public Object getOrderByUserId(@PathVariable Long userId, Integer state) {
        return orderService.getOrderByUserId(userId, state);
    }

    /**
     * 删除购物车中的商品
     *
     * @param id 商品id
     */
    @DeleteMapping("/{id}")
    public void deleteOrder(@PathVariable Long id) {
        orderService.deleteOrder(id);
    }

    /**
     * 结算购物车
     */
    @PostMapping("/updateBatch")
    public Object updateBatch(Long[] ids, Integer state, Integer totalIntegral, Long userId) {
        JasonResult result = new JasonResult();
        try {
            orderService.updateBatch(ids, state, totalIntegral, userId);
        } catch (Exception e) {
            result.mark(e.getMessage());
        }
        return result;
    }

}
